var taskmanager = {
    oTable: $('#table'),
    searchOk: $("#ok"),
    resetSearch: $("#reset"),
    init: function () {
        jeDate({
            dateCell: '#begin_at',
            format: 'YYYY-MM-DD',
            festival: true,
            maxDate: '2099-06-16 23:59:59', //最大日期
            isTime: true
        });
        jeDate({
            dateCell: '#end_at',
            format: 'YYYY-MM-DD',
            festival: true,
            maxDate: '2099-06-16 23:59:59', //最大日期
            isTime: true
        });
        jeDate({
            dateCell: '#endtime',
            format: 'YYYY-MM-DD',
            festival: true,
            maxDate: '2099-06-16 23:59:59', //最大日期
            isTime: true
        });
        this.initConfigTable();
        $("#taskdetalmodal").on("hidden.bs.modal", function () {
            $(this).removeData("bs.modal");
        });
    },
    initConfigTable: function () {
        var _self = this;
        this.oTable.bootstrapTable({
            url: "/merchant/ajaxtasklist.html",
            method: 'get',
            toolbar: '#toolbar',
            queryParams: this.queryParams,
            pagination: true, //是否分页
            pageSize: 20, //单页记录数
            pageList: [20, 50, 100, 200], //分页步进值
            sidePagination: "server", //服务端分页
            columns: [
                {
                    title: "任务ID",
                    field: "id"
                },
                {
                    title: "任务标题",
                    field: "title"
                },
                {
                    title: "总需求量/隐藏",
                    field: "number-hidcount"
                },
                {
                    title: "已完成",
                    field: "stacount"
                },
                {
                    title: "不合格",
                    field: "comcount"
                },
                {
                    title: "任务状态",
                    field: "status"
                },
                {
                    title: "初始发布量",
                    field: "nosheetcount"
                },
                {
                    title: "发布时间",
                    field: "created_at"
                },
                {
                    title: "管理操作",
                    field: "action",
                    class: "table-action-class"
                }
            ]
        });
        this.searchOk.bind("click", function () {
            _self.oTable.bootstrapTable("refresh", {pagination: true});
        });
        this.resetSearch.bind("click", function () {
            $('#toolbar').find('input[name]').each(function () {
                $(this).val("");
            });
            $('#toolbar').find('select[name]').each(function () {
                $(this).val("");
                $('.selectpicker').selectpicker('val', '-1');
            });
            _self.oTable.bootstrapTable("refresh", {pagination: true});
        });
    },
    queryParams: function (params) {
        $('#toolbar').find('input[name]').each(function () {
            params[$(this).attr('name')] = $(this).val();
        });
        $('#toolbar').find('select[name]').each(function () {
            params[$(this).attr('name')] = $(this).val();
        });
        return params;
    },
    taskdetail: function (taskid) {
        var _self = this;
        $.post("/merchant/taskdetail.html", {id: taskid}, function (e) {
            if (e.status === true) {
                for (var item in e.data) {
                    if (item === "sample_figure" || item === "qrcode") {
                        $("#taskdetalmodal #" + item).attr("src", e.data[item]);
                    } else if (item === "is_offer_mobile") {
                        $("#taskdetalmodal #" + item).text(e.data[item] == 0 ? "否" : "是");
                    } else {
                        $("#taskdetalmodal #" + item).text(e.data[item]);
                        $("#taskdetalmodal-toptask #" + item).text(e.data[item]);
                        $("#taskdetalmodal-addtask #" + item).text(e.data[item]);
                        $("#taskdetalmodal-reflashtask #" + item).text(e.data[item]);
                        $("#taskdetalmodal-edittask #" + item).text(e.data[item]);
                        if (item === "title" || item === "vote_address" || item ==="sample_figure_count") {
                            $("#taskdetalmodal-edittask #" + item).val(e.data[item]);
                        }
                    }
                }
                $("#taskdetalmodal").modal("show");
                $("#toptask").unbind("click").bind("click", function () {
                    setTimeout(function () {
                        _self.toptask(taskid);
                    }, 500);
                });
                $("#addtask").unbind("click").bind("click", function () {
                    setTimeout(function () {
                        _self.addtask(taskid);
                    }, 500);
                });
                $("#reflashtask").unbind("click").bind("click", function () {
                    setTimeout(function () {
                        _self.reflashtask(taskid);
                    }, 500);
                });
                $("#edittask").unbind("click").bind("click", function () {
                    setTimeout(function () {
                        _self.edittask(taskid);
                    }, 500);
                });
            }
        }, "json");
    },
    toptask: function (taskid) {
        var $modal = $("#taskdetalmodal-toptask"),
                _self = this;
        $modal.modal("toggle");
        $(".btn-toptask", $modal).unbind("click").bind("click", function () {
            var isdedu = $("input[type='radio']:checked", $modal).val();
            $.post("/merchant/toptask.html", {id: taskid, isdedu: isdedu}, function (e) {
                $modal.modal("hide");
                if (e.status === true) {
                    _self.oTable.bootstrapTable("refresh", {pagination: true});
                    $.alert({
                        title: false,
                        content: e.message,
                        confirmButtonClass: 'btn-consult-alert'
                    });
                } else {
                    $.alert({
                        title: false,
                        content: e.message,
                        confirmButtonClass: 'btn-consult-alert'
                    });
                }
            }, "json");
        });
    },
    addtask: function (taskid) {
        var $modal = $("#taskdetalmodal-addtask"),
                _self = this;
        $modal.modal("toggle");
        $(".btn-addtask", $modal).unbind("click").bind("click", function () {
            var isdedu = $("input[type='radio']:checked", $modal).val(),
                    end_at = $("input[name='end_at']", $modal).val(),
                    number = $("input[name='number']", $modal).val(),
                    price = $("#price", $modal).text();
            $.post("/merchant/addtask.html", {id: taskid, isdedu: isdedu, number: number, end_at: end_at, price: price}, function (e) {
                $modal.modal("hide");
                if (e.status === true) {
                    _self.oTable.bootstrapTable("refresh", {pagination: true});
                    $.alert({
                        title: false,
                        content: e.message,
                        confirmButtonClass: 'btn-consult-alert'
                    });
                } else {
                    $.alert({
                        title: false,
                        content: e.message,
                        confirmButtonClass: 'btn-consult-alert'
                    });
                }
            }, "json");
        });
    },
    reflashtask: function (taskid) {
        var $modal = $("#taskdetalmodal-reflashtask"),
                _self = this;
        $modal.modal("toggle");
        $(".btn-reflashtask", $modal).unbind("click").bind("click", function () {
            var isdedu = $("input[type='radio']:checked", $modal).val(),
                    reflashNO = $("input[name='reflashNO']", $modal).val();
            $.post("/merchant/reflashtask.html", {id: taskid, isdedu: isdedu, reflashNO: reflashNO}, function (e) {
                $modal.modal("hide");
                if (e.status === true) {
                    _self.oTable.bootstrapTable("refresh", {pagination: true});
                    $.alert({
                        title: false,
                        content: e.message,
                        confirmButtonClass: 'btn-consult-alert'
                    });
                } else {
                    $.alert({
                        title: false,
                        content: e.message,
                        confirmButtonClass: 'btn-consult-alert'
                    });
                }
            }, "json");
        });
    },
    edittask: function (taskid) {
        $("#qrpickfiles").fileinput({
            showCaption: false,
            browseClass: "btn btn-danger",
            browseLabel: "上传样图",
            browseIcon: "<i class=\"glyphicon glyphicon-picture\"></i> ",
            showRemove: false,
            showUpload: false
        });
        $("#qrpickfilesqrcode").fileinput({
            showCaption: false,
            browseClass: "btn btn-danger",
            browseLabel: "点击上传二维码",
            browseIcon: "<i class=\"glyphicon glyphicon-picture\"></i> ",
            showRemove: false,
            showUpload: false
        });
         var $modal = $("#taskdetalmodal-edittask"),
                _self = this;
        $modal.modal("toggle");
        $(".btn-edittask", $modal).unbind("click").bind("click", function () {
            $("#id", $modal).val(taskid);
            $("#editform").submit();
        });
    },
    stoptask: function (taskid) {
        var $modal = $("#taskdetalmodal-stoptask"),
                _self = this;
        $modal.modal("show");
        $(".btn-stoptask", $modal).unbind("click").bind("click", function () {
            var stopcashback = $("input[type='radio']:checked", $modal).val();
            $.post("/merchant/stoptask.html", {id: taskid, stopcashback: stopcashback}, function (e) {
                $modal.modal("hide");
                if (e.status === true) {
                    _self.oTable.bootstrapTable("refresh", {pagination: true});
                    $.alert({
                        title: false,
                        content: e.message,
                        confirmButtonClass: 'btn-consult-alert'
                    });
                } else {
                    $.alert({
                        title: false,
                        content: e.message,
                        confirmButtonClass: 'btn-consult-alert'
                    });
                }
            }, "json");
        });
    },
    cashbacktask: function (taskid) {
        var _self = this;
        $.confirm({
            content: '是否确定将强制终止任务的剩余返现？',
            title: "提示",
            cancelButton: "关闭",
            confirmButton: "确定",
            confirmButtonClass: 'btn-consult',
            cancelButtonClass: 'btn-consult',
            confirm: function () {
                $.post("/merchant/cashbacktask.html", {id: taskid}, function (e) {
                    if (e.status === true) {
                        _self.oTable.bootstrapTable("refresh", {pagination: true});
                        $.alert({
                            title: false,
                            content: e.message,
                            confirmButtonClass: 'btn-consult-alert'
                        });
                    } else {
                        $.alert({
                            title: false,
                            content: e.message,
                            confirmButtonClass: 'btn-consult-alert'
                        });
                    }
                }, "json");
            }
        });
    }
};

